package com.wisewin.pose.api.controller;
import com.wisewin.pose.api.controller.base.BaseController;
import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.entity.bo.QueryParamBO;
import com.wisewin.pose.entity.db.UserDO;
import com.wisewin.pose.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@PostMapping("/queryUserById")
	public ResultDTO queryUserById (@RequestBody UserDO userDO) {
		return userService.queryUserById(userDO.getId());
	}

	// 根据微信公众号获取信息
	@PostMapping("/getAuthUserInfo")
	public ResultDTO getAuthUserInfo (@RequestBody QueryParamBO queryParamBO) {
		return userService.getAuthUserInfo(queryParamBO.getCode());
	}

	// 获取token
	@PostMapping("/getToken")
	public ResultDTO getToken() {
		return userService.getToken();
	}

	@GetMapping("/queryByPhone")
	public ResultDTO queryByPhone (QueryParamBO queryParamBO) {
		UserDO userDO = userService.queryByPhone(queryParamBO.getPhone());
		String sessionID = super.setLoginUser(userDO);
		userDO.setSessionId(sessionID);
		return ResultDTOBuilder.success(userDO);
	}

	@PostMapping("/updateUser")
	public ResultDTO updateUser (@RequestBody UserDO userDO) {
		if (StringUtils.isEmpty(userDO.getSessionId())) {
			return ResultDTOBuilder.failure("0000005");
		}
		// 获取登录用户
		UserDO loginUser = super.getLoginUser(userDO.getSessionId());
		if (loginUser == null) { // 判断是否登录
			return ResultDTOBuilder.failure("0000001");
		}
		userDO.setId(loginUser.getId());
		return userService.updateUser(userDO);
	}
}
